/**
 *
 */
package vn.tecviets.thuandt.controller.action.post;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vn.tecviets.thuandt.model.bean.Category;
import vn.tecviets.thuandt.model.bo.CategoryBO;
import vn.tecviets.thuandt.model.bo.PostBO;
import vn.tecviets.thuandt.model.form.LoginActionForm;
import vn.tecviets.thuandt.model.form.PostActionForm;

/*
 * CreatePostAction
 * 1.0
 * Mar 19, 2013
 * 
 * Copyright notice
 * 
 * Modification Logs:
 * 
 * DATE					AUTHOR				DESCRIPTION
 * -----------------------------------------------------------
 * Mar 19, 2013			Thuandt				Create
 */

public class CreatePostAction extends Action {
	
	PostBO postBO = new PostBO();
	CategoryBO categoryBO = new CategoryBO();
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		
		/*
		 * Check login session
		 */
		LoginActionForm loginActionForm = (LoginActionForm) request.getSession().getAttribute("loginActionForm");
		if (loginActionForm == null) {
			return mapping.findForward("login");
		}
	
		/*
		 * Create data for Category dropdown list
		 */
		ArrayList<Category> categoryArrayList;
		try {
			categoryArrayList = categoryBO.getAllCategory();
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
			throw e1;
		} catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
		}
		request.setAttribute("categoryArrayList", categoryArrayList);
		
		
		/*
		 * Check whether it is the first time the page is load
		 */
		if (request.getParameter("submit") == null) {
			return mapping.findForward("create");
		}
		
		/*
		 * Get data of the new post
		 */
		PostActionForm postActionForm = (PostActionForm) form;
		String title = postActionForm.getTitle();
		int categoryId = postActionForm.getCategoryId();
		String image = postActionForm.getImage();
		String caption = postActionForm.getCaption();
		String summary = postActionForm.getSummary();
		String showed = postActionForm.getShowed();
		String important = postActionForm.getImportant();
		String username = loginActionForm.getUsername();
		String content = postActionForm.getContent();
		
		/*
		 * Create pathname of new file, which will store content of new post
		 */
		String pathname = getServlet().getServletContext().getRealPath("\\Posts") + "\\" + title + ".html";
		
		/*
		 * Check whether some require info of a new post are inputted or not
		 */
		ActionErrors actionErrors = postBO.validateCreate(title, categoryId, image, caption, summary, content, pathname);
		saveErrors(request, actionErrors);
		int actionErrorsSize = actionErrors.size();
		if (actionErrorsSize != 0) {
			return mapping.findForward("create");
		}
			
		
		/*
		 * Add info of new post into database and save content of new post into a new file
		 */
		try {
			postBO.createPost(title, categoryId, image, caption, summary, showed, important, username, content, pathname);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw e;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		
		return mapping.findForward("posts");
	}
}	
